#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
pthread_mutex_t cntr_mutex = PTHREAD_MUTEX_INITIALIZER;//创建一个互斥变量，快速互斥
long protVariable = 0L;
int j = 0;
void *myThread(void * arg){
	int i, ret;
	printf("我跑起来啦，我是%d\n",(int)arg);
	for(i=0; i<10000; i++){
		//  P V 操作
		ret = pthread_mutex_lock( &cntr_mutex );
		if(ret != 0){
			j++;
		}
		assert( ret == 0 );
		protVariable++;
		ret = pthread_mutex_unlock( &cntr_mutex );
		assert( ret == 0 );
	}
	pthread_exit(NULL);
}
#define MAX_THREADS 10
int main(){
	int ret, i;
	pthread_t threadIds[MAX_THREADS];
	for(i=0; i<MAX_THREADS; i++){
		ret = pthread_create( &threadIds[i], NULL, myThread, (void *)i);
		if(ret != 0){
			printf("Error creating thread %d\n", (int)threadIds[i]);
		}
	}
	for(i = 0; i<MAX_THREADS; i++){
		ret = pthread_join( threadIds[i], NULL);
		if(ret != 0){
			printf("Error joining thread %d\n", (int)threadIds[i]);
		}
	}
	printf("The protected variable value is %ld\n", protVariable);
	printf("互斥的次数为 %d",j);
	ret = pthread_mutex_destroy( & cntr_mutex );
	if(ret != 0 ){
		printf("Could not destroy the mutex\n");
	}
	return 0;
}
